package io.renren.adaptor;

import java.util.Random;
import java.util.concurrent.TimeUnit;

public class B {

    //最大重试次数 10
    private static  final Integer tryTimes=10;
    //重时间隔单位秒
    private static  final  Integer intervalTime = 2;

    public static void main(String[] args) throws InterruptedException {
        boolean b = B.retryBuss();
        System.out.println("最终指向结果:"+(b ? "成功":"失败"));
    }

    private static boolean retryBuss() throws InterruptedException {
        Integer retryNum = 1;
        boolean flag = false;
        while (retryNum <= tryTimes){
            try {
                flag = execute(retryNum);
                if(flag){
                    System.out.println("第"+retryNum+"成功");
                    break;
                }
                System.out.println("第"+retryNum+"失败");
                TimeUnit.SECONDS.sleep(intervalTime);
                retryNum++;
            }catch (Exception e){
                retryNum++;
                continue;
            }

        }
        return flag;
    }

    /**
     * 具体得业务逻辑
     * @param retryNum
     * @return
     */
    private static boolean execute(int retryNum) {
        Random random = new Random();
        int i = random.nextInt(20);
        boolean flag = true;
        try {
            if(i == 6 ){
                flag = false;
                throw  new RuntimeException();
            }
        }catch (Exception e){

        }

        return flag;
    }

}